//
//  YJDeviceManager.m
//  UTEDemo
//
//  Created by caofuqing on 2022/9/20.
//  Copyright © 2022 vv. All rights reserved.
//

#import "YJDeviceManager.h"
#import "YjHealthPlugin.h"
#import "MJExtension.h"

@interface YJDeviceManager ()
@property (nonatomic,strong) SmartBandDelegateTool      *smartBandTool;
@end

@implementation YJDeviceManager
#pragma mark - init
+ (YJDeviceManager *)sharedInstance {
    static YJDeviceManager *instance = nil;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        instance = [[self alloc] init];
    });
    return instance;
}
- (instancetype)init {
    self = [super init];
    if (!self) {
        return nil;
    }
    return self;
}
/// 1.初始化SDK
- (void)initSdk {
    //CN:实例化
    //EN:Instantiate
    self.smartBandMgr = [UTESmartBandClient sharedInstance];
    
    //CN:初始化
    //EN:Initialization
    [self.smartBandMgr initUTESmartBandClient];
    
    //CN:打印日志
    //EN:Print log
    self.smartBandMgr.debugUTELog = YES;
    
    //CN:重复扫描设备(这样设备信号值才会实时变化)
    //EN:Scan the device repeatedly (so that the device signal value will change in real time)
    self.smartBandMgr.isScanRepeat = YES;
    
    //CN:设置信号值过滤
    //EN:Set signal value filtering
    self.smartBandMgr.filerRSSI = -60;
    
    //CN:设置过滤服务（可以搜索包含设置服务的设备）
    //EN:Setting filter service (you can search for devices that include setting service)
    [self.smartBandMgr setFilerServers:@[@"5533"]];
//    self.smartBandMgr.isScanAllDevice = YES;

//    [self.smartBandMgr setFilerServers:@[@"5533",@"2222",@"FEE7"]];

    NSLog(@"sdk vsersion =%@",self.smartBandMgr.sdkVersion);
    
    self.smartBandTool = [[SmartBandDelegateTool alloc] init];
    self.smartBandMgr.delegate = self.smartBandTool;
    
    __weak typeof(self)weakSelf = self;
    self.smartBandTool.findDevicesBlock = ^{
//        dispatch_async(dispatch_get_main_queue(), ^(void) {
//            [weakSelf.tableView reloadData];
//        });
        NSLog(@"caofuqing--%ld",weakSelf.smartBandTool.mArrayDevices.count);
        if (self.smartBandTool.mArrayDevices.count > 0) {
            NSMutableArray *d = [[NSMutableArray alloc] init];
            for (UTEModelDevices *model in weakSelf.smartBandTool.mArrayDevices) {
                NSMutableDictionary *dict = model.mj_keyValues;
                [dict removeObjectsForKeys:@[@"advertisementData", @"mServicesUUID"]];
                [d addObject:dict];
            }
            NSDictionary *dic = @{@"list" : d};
            [[YjHealthPlugin sharedInstance] sendToFlutterName:yjCustomTypeName tag:yjCustomTypeScan dict:dic];
        }
    };
    NSDictionary *dic = @{};
    [[YjHealthPlugin sharedInstance] sendToFlutterName:yjCustomTypeName tag:yjCustomTypeInitSdk dict:dic];
}
/// 2.开始搜设备
- (void)startScanDevices {
    [self.smartBandMgr startScanDevices];
}
/// 3.停止搜设备
- (void)stopScanDevices {
    [self.smartBandMgr stopScanDevices];
}
/// 4.链接设备
- (void)connect {
    if ([YjHealthPlugin sharedInstance].identifier && [YjHealthPlugin sharedInstance].identifier.length > 0) {
        [self stopScanDevices];
        UTEModelDevices *model = [[UTEModelDevices alloc] init];
        model.identifier = [YjHealthPlugin sharedInstance].identifier;
        [self.smartBandMgr connectUTEModelDevices:model];
    } else {
        NSLog(@"蓝牙的地址是空！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！");
    }
    /*
    if (self.smartBandTool.mArrayDevices.count == 0) {
        NSLog(@"***didSelectRowAtIndexPath-------mArrayDevices array nil");
        return;
    }
    
    UTEModelDevices *model = nil;
    for (UTEModelDevices *m in self.smartBandTool.mArrayDevices) {
        if ([m.advertisementAddress isEqualToString:address]) {
            model = m;
            break;
        }
    }
    if (model == nil) {
        return;
    }
    [self.smartBandMgr connectUTEModelDevices:model];
    */
}
/// 5.主动断开设备
- (void)disconnect {
    UTEModelDevices *model = self.smartBandMgr.connectedDevicesModel;
    [self.smartBandMgr disConnectUTEModelDevices:model];
}
/// 6.设置部分设备信息
- (void)setUTEOption:(UTEOption)option {
    [self.smartBandMgr setUTEOption:option];
}
/// 7.同步数据(从某个日期到现在)
- (void)syncDataCustomTime:(NSString *_Nonnull)time type:(UTEDeviceDataType)type {
    [self.smartBandMgr syncDataCustomTime:time type:type];
}
/// 8.同步数据(获取特定开始时间的GPS数据)
- (void)syncDataGPS:(NSString *_Nonnull)time {
    [self.smartBandMgr syncDataGPS:time];
}
/// 9.同步数据(从某一天到今天的所有体育数据)
- (void)syncUTESportModelCustomTime:(NSString *_Nonnull)time {
    [self.smartBandMgr syncUTESportModelCustomTime:time];
}
@end
